Exporting OleObject into word document

I need to export oleobjects into microsoft word as a normal table not as an ole object.

 

 


dhars_priya - Wed May 20 08:23:43 EDT 2015

Re: Exporting OleObject into word document
woodpryan - Wed May 20 11:05:33 EDT 2015

We need more information than that. This isn't even a question. Please elaborate.

Re: Exporting OleObject into word document
dhars_priya - Thu May 21 00:18:14 EDT 2015

woodpryan - Wed May 20 11:05:33 EDT 2015

We need more information than that. This isn't even a question. Please elaborate.

Hi woodpryan,

My question is,

Requirement  is to provide a script which searches for objects with exact one Word Table cell, copies the cell content (take case of OLE objects!), removes the table, pastes the table content, removes the indents (2x), removes empty lines at the end.

I used oleCopy(o) which copied whole object , i need to get only the contents of it.I posted this question like is it possible to take only the oleobject content some said it is not possible but it can be done threw native application.

So i thought to have dxl script or VBA to write code to export the module to word which converts oleobject as tables. and then i can get the contents right. Is this can be a solution ?

Is it possible to export the oleobjects in DOORS module as normal table in word?

 

Re: Exporting OleObject into word document
dhars_priya - Thu May 21 00:21:45 EDT 2015

woodpryan - Wed May 20 11:05:33 EDT 2015

We need more information than that. This isn't even a question. Please elaborate.

I am new to dxl scripting, Could you provide any solution to this please.

Thanks.

Re: Exporting OleObject into word document
woodpryan - Thu May 21 15:08:43 EDT 2015

dhars_priya - Thu May 21 00:21:45 EDT 2015

I am new to dxl scripting, Could you provide any solution to this please.

Thanks.

Using OleCopy(obj), you've already copied the table itself to the system clipboard. If you already have a handle on the MS Word application, the active document and the current selection (objSel) at the time, you can then call the paste method using oleMethod(objSel, cMethodPaste).

So, now you've pasted the Table. All you have to do at that point is get the table from the document as an oleAutoObj and you can retrieve the contents of it's first cell. Just take that text and set the Object's "Object Text" to be the text by itself.

Seems to me that would work.

Re: Exporting OleObject into word document
dhars_priya - Fri May 22 08:27:39 EDT 2015

woodpryan - Thu May 21 15:08:43 EDT 2015

Using OleCopy(obj), you've already copied the table itself to the system clipboard. If you already have a handle on the MS Word application, the active document and the current selection (objSel) at the time, you can then call the paste method using oleMethod(objSel, cMethodPaste).

So, now you've pasted the Table. All you have to do at that point is get the table from the document as an oleAutoObj and you can retrieve the contents of it's first cell. Just take that text and set the Object's "Object Text" to be the text by itself.

Seems to me that would work.

Thanks woodpryan. I copied and pasted the oleobject in the word.

But I am getting error like object table is null , but in the document the oleobject table is present.

What is the reason?

And another question is , i need to take only the ole object having one cell how would i able to find that the object table has only one cell?

Re: Exporting OleObject into word document
dhars_priya - Mon May 25 06:10:42 EDT 2015

woodpryan - Thu May 21 15:08:43 EDT 2015

Using OleCopy(obj), you've already copied the table itself to the system clipboard. If you already have a handle on the MS Word application, the active document and the current selection (objSel) at the time, you can then call the paste method using oleMethod(objSel, cMethodPaste).

So, now you've pasted the Table. All you have to do at that point is get the table from the document as an oleAutoObj and you can retrieve the contents of it's first cell. Just take that text and set the Object's "Object Text" to be the text by itself.

Seems to me that would work.

could you please tell me how to get the values from the ole object table?

Re: Exporting OleObject into word document
dhars_priya - Tue May 26 05:25:51 EDT 2015

woodpryan - Thu May 21 15:08:43 EDT 2015

Using OleCopy(obj), you've already copied the table itself to the system clipboard. If you already have a handle on the MS Word application, the active document and the current selection (objSel) at the time, you can then call the paste method using oleMethod(objSel, cMethodPaste).

So, now you've pasted the Table. All you have to do at that point is get the table from the document as an oleAutoObj and you can retrieve the contents of it's first cell. Just take that text and set the Object's "Object Text" to be the text by itself.

Seems to me that would work.

I am not able to get the ole object table values.

Could you give sample code for how to get the contents.

Re: Exporting OleObject into word document
woodpryan - Thu May 28 11:21:02 EDT 2015

dhars_priya - Tue May 26 05:25:51 EDT 2015

I am not able to get the ole object table values.

Could you give sample code for how to get the contents.

Sorry it has taken me a while to get back to you. I've been out of work sick for the last few days.

So, you said you have been able to get the table pasted into Word. Now you need a handle on the actual table in Word. The following function will retrieve a table from the Word collection of tables having a certain index. If you do all this one table at a time, I suppose the index will always be 1 or 0. I think the index to the collection begins with 1, not 0.

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

Now that you have the Word table, you need a range with the cell from which you want to retrieve a value. The following function will return the range you need given the start and end cell number (which, in your case, will be the same).

 

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

Now that you have a range representing the cell from which you want the text... you need to get the text. The following function will take a range and return the text that is in the range.
 

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

Now that you have the text of the table, I'm sure you should be able to place the plain text returned from the table cell into the "Object Text" of the original Object.

If you are doing all this in a loop, you will probably want to delete the table you already took care of from the document before you move on to the next table. The following functions, called one after the other, will delete all the tables in the Word document.
 

Call the expandSelection function, passing it wdStory

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        endLogging();
        halt();
    }
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

Then call deleteTablesInSelection

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

I wish I could have seen some code from you, my friend. An effort. That's alright, though. I hope this works for you. Let me know if you have any trouble with it.

Re: Exporting OleObject into word document
dhars_priya - Fri May 29 06:25:59 EDT 2015

woodpryan - Thu May 28 11:21:02 EDT 2015

Sorry it has taken me a while to get back to you. I've been out of work sick for the last few days.

So, you said you have been able to get the table pasted into Word. Now you need a handle on the actual table in Word. The following function will retrieve a table from the Word collection of tables having a certain index. If you do all this one table at a time, I suppose the index will always be 1 or 0. I think the index to the collection begins with 1, not 0.

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

Now that you have the Word table, you need a range with the cell from which you want to retrieve a value. The following function will return the range you need given the start and end cell number (which, in your case, will be the same).

 

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

Now that you have a range representing the cell from which you want the text... you need to get the text. The following function will take a range and return the text that is in the range.
 

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

Now that you have the text of the table, I'm sure you should be able to place the plain text returned from the table cell into the "Object Text" of the original Object.

If you are doing all this in a loop, you will probably want to delete the table you already took care of from the document before you move on to the next table. The following functions, called one after the other, will delete all the tables in the Word document.
 

Call the expandSelection function, passing it wdStory

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        endLogging();
        halt();
    }
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

Then call deleteTablesInSelection

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

I wish I could have seen some code from you, my friend. An effort. That's alright, though. I hope this works for you. Let me know if you have any trouble with it.

Hi woodpryan.. Thanks for your code.

I wrote a code which tells like null objtype was passed.

Then i thought , i copy and pasted the oleobject in dxl and through vba i copied the single celled table and pasted in textfile.

I am working on to paste the copied content to dxl.

I also tried the above code , same problem is like objtable is null why i dont no but i have an oleobject table in word.

  oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }

I am getting null objType .. Could you tell me what i made wrong?

 

Re: Exporting OleObject into word document
woodpryan - Fri May 29 10:24:38 EDT 2015

dhars_priya - Fri May 29 06:25:59 EDT 2015

Hi woodpryan.. Thanks for your code.

I wrote a code which tells like null objtype was passed.

Then i thought , i copy and pasted the oleobject in dxl and through vba i copied the single celled table and pasted in textfile.

I am working on to paste the copied content to dxl.

I also tried the above code , same problem is like objtable is null why i dont no but i have an oleobject table in word.

  oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }

I am getting null objType .. Could you tell me what i made wrong?

 

I can't entirely be sure what's wrong, but I am wondering, did you put anything into objArgBlock?

Think about DXL as a wrapper around VBA in this scenario. For each VBA property you need to get, you call oleGet. For example, the following VBA code:

rng = Selection.Range

translates into the following DXL code

oleGet(objSel, cPropertyRange, objRange);

For each VBA property you want to set, you need to call oleSet. For example, the following VBA code:

Selection.Font.Name = "Arial"

translates into the folowing DXL code:

oleGet(objSel, cPropertyFont, objFont);

olePut(objFont, cPropertyName, "Arial");

Finally, for each VBA method you want to call, you need to call oleMethod. Pass it the VBA Object on which you want to call the function, the name of the function to call, a list of arguments to pass into the VBA function, and an Object into which you need to store the return value. For example, the following VBA code:

Dim tbl As Table

tbl = Selection.Tables.Item(1)

translates into the following DXL code:

oleGet(objSel, cPropertyTables, objTables)

clear(objArgBlock);

put(objArgBlock, 1);

oleMethod(objTables, cMethodItem, objArgBlock, objTable)

 

You can't call a VBA method that takes arguments without passing it the required arguments. Always remember to clear your argument block then put the arguments to the VBA function into the objArgBlock. Keeping these things in mind, you should be able to write DXL code to do literally ANYTHING that you can do in VBA.

Re: Exporting OleObject into word document
dhars_priya - Mon Jun 01 03:31:17 EDT 2015

woodpryan - Fri May 29 10:24:38 EDT 2015

I can't entirely be sure what's wrong, but I am wondering, did you put anything into objArgBlock?

Think about DXL as a wrapper around VBA in this scenario. For each VBA property you need to get, you call oleGet. For example, the following VBA code:

rng = Selection.Range

translates into the following DXL code

oleGet(objSel, cPropertyRange, objRange);

For each VBA property you want to set, you need to call oleSet. For example, the following VBA code:

Selection.Font.Name = "Arial"

translates into the folowing DXL code:

oleGet(objSel, cPropertyFont, objFont);

olePut(objFont, cPropertyName, "Arial");

Finally, for each VBA method you want to call, you need to call oleMethod. Pass it the VBA Object on which you want to call the function, the name of the function to call, a list of arguments to pass into the VBA function, and an Object into which you need to store the return value. For example, the following VBA code:

Dim tbl As Table

tbl = Selection.Tables.Item(1)

translates into the following DXL code:

oleGet(objSel, cPropertyTables, objTables)

clear(objArgBlock);

put(objArgBlock, 1);

oleMethod(objTables, cMethodItem, objArgBlock, objTable)

 

You can't call a VBA method that takes arguments without passing it the required arguments. Always remember to clear your argument block then put the arguments to the VBA function into the objArgBlock. Keeping these things in mind, you should be able to write DXL code to do literally ANYTHING that you can do in VBA.

Hi woodpryan ,

Sorry for disturbing...

This is the code i have written,

//#include <utils/ole.inc>
const string cPropertyCount ="Count"
const string cPropertyTables ="Tables"
const string cMethodItem="Item"
const string cMethodCell="Cell"
Object o
Module m=current
int rowNo=0
int ColNo=0
string result
int count
string sCellText
OleAutoObj cobjWord
OleAutoObj objSel=null
OleAutoObj objDocs=null
OleAutoObj objDoc=null
OleAutoObj objTables=null
OleAutoObj objTable=null
OleAutoObj  objCell=null
OleAutoObj  objRange=null
OleAutoObj objFind=null
   OleAutoObj objRows
   OleAutoObj objCol
OleAutoArgs oleAutoArgs=create
cobjWord=oleCreateAutoObject("Word.Application")
if(null cobjWord)
{
warningBox "Unable to start Microsoft Word"
halt
}//28
olePut(cobjWord, "Visible", true)

for o in m do
{
if(oleCopy(o))
{
print "copied""\n"
oleGet(cobjWord, "Documents", objDocs)
oleMethod(objDocs, "Add",oleAutoArgs,objDoc)
oleGet(cobjWord, "Selection", objSel)
clear(oleAutoArgs);

oleMethod(objSel, "Paste")

 oleGet(objSel,cPropertyTables, objTables)

 
if(null objTables)
print "no table""\n"
else
print "table""\n"
clear oleAutoArgs
put(oleAutoArgs,1)
oleGet(objTables,cMethodItem, oleAutoArgs, objTable);

if(null objTable)
print "null table""\n"
}
}

 

 

I am getting the null objtable why i dont no.. I am not able to understand what i made wrong.

Re: Exporting OleObject into word document
woodpryan - Mon Jun 01 11:01:37 EDT 2015

dhars_priya - Mon Jun 01 03:31:17 EDT 2015

Hi woodpryan ,

Sorry for disturbing...

This is the code i have written,

//#include <utils/ole.inc>
const string cPropertyCount ="Count"
const string cPropertyTables ="Tables"
const string cMethodItem="Item"
const string cMethodCell="Cell"
Object o
Module m=current
int rowNo=0
int ColNo=0
string result
int count
string sCellText
OleAutoObj cobjWord
OleAutoObj objSel=null
OleAutoObj objDocs=null
OleAutoObj objDoc=null
OleAutoObj objTables=null
OleAutoObj objTable=null
OleAutoObj  objCell=null
OleAutoObj  objRange=null
OleAutoObj objFind=null
   OleAutoObj objRows
   OleAutoObj objCol
OleAutoArgs oleAutoArgs=create
cobjWord=oleCreateAutoObject("Word.Application")
if(null cobjWord)
{
warningBox "Unable to start Microsoft Word"
halt
}//28
olePut(cobjWord, "Visible", true)

for o in m do
{
if(oleCopy(o))
{
print "copied""\n"
oleGet(cobjWord, "Documents", objDocs)
oleMethod(objDocs, "Add",oleAutoArgs,objDoc)
oleGet(cobjWord, "Selection", objSel)
clear(oleAutoArgs);

oleMethod(objSel, "Paste")

 oleGet(objSel,cPropertyTables, objTables)

 
if(null objTables)
print "no table""\n"
else
print "table""\n"
clear oleAutoArgs
put(oleAutoArgs,1)
oleGet(objTables,cMethodItem, oleAutoArgs, objTable);

if(null objTable)
print "null table""\n"
}
}

 

 

I am getting the null objtable why i dont no.. I am not able to understand what i made wrong.

OK. Now we've got some code to work with. First, there is no need to add a new document to the Word Application in your loop. Keep the following handles as global and never change them: cobjWord, objDocs, objDoc, and objSel. Second, you are attempting to retrieve the Table out of the Selection Object. At the time you attempt to retrieve the Table, the selection is likely at the bottom of the document. If you were to get the 'count' property from the Tables collection, you would probably find it to be zero. So, get the Tables collection from objDoc, not objSel. After you have done all you need with the table, delete it. Inside your loop, you should be getting/pasting the table, getting the data from the table and putting it into your DOORS Object, then deleting the table from the document. Round and round it goes. There is no need to re-get objDocs, objDoc, or objSel inside your loop. To delete the Tables, you can call the function expandSelection and pass it wdStory. Remember that function is posted in here above. Then call the deleteTablesInSelection function that is also posted above.

 

Finally, please surround your code in the tags that are provided for code. It is otherwise quite difficult to read. Keep the code coming in your responses, though. I appreciate that. This was much more productive than our previous interactions.

Re: Exporting OleObject into word document
dhars_priya - Tue Jun 02 05:00:27 EDT 2015

woodpryan - Mon Jun 01 11:01:37 EDT 2015

OK. Now we've got some code to work with. First, there is no need to add a new document to the Word Application in your loop. Keep the following handles as global and never change them: cobjWord, objDocs, objDoc, and objSel. Second, you are attempting to retrieve the Table out of the Selection Object. At the time you attempt to retrieve the Table, the selection is likely at the bottom of the document. If you were to get the 'count' property from the Tables collection, you would probably find it to be zero. So, get the Tables collection from objDoc, not objSel. After you have done all you need with the table, delete it. Inside your loop, you should be getting/pasting the table, getting the data from the table and putting it into your DOORS Object, then deleting the table from the document. Round and round it goes. There is no need to re-get objDocs, objDoc, or objSel inside your loop. To delete the Tables, you can call the function expandSelection and pass it wdStory. Remember that function is posted in here above. Then call the deleteTablesInSelection function that is also posted above.

 

Finally, please surround your code in the tags that are provided for code. It is otherwise quite difficult to read. Keep the code coming in your responses, though. I appreciate that. This was much more productive than our previous interactions.

When i didnt add this code means oleMethod(objDocs, "Add",oleAutoArgs,objDoc) i am getting only blank page that is edit text is not viewed.

oleMethod(objTables,cMethodItem, oleAutoArgs, objTable);  IN this code error occurs like as below.

OLE problem: The requested member of the collection does not exist., OLE error code: -2146822347

I changed the code whatever you said.

Now also i am getting count as zero.

#include <utils/ole.inc>

 cobjWord=oleCreateAutoObject("Word.Application")
 if(null cobjWord)
     {
 warningBox "Unable to start Microsoft Word"
 halt
     }
 olePut(cobjWord, "Visible", true)
oleGet(cobjWord, "Documents", objDocs)

oleMethod(objDocs, "Add")

oleGet (cobjWord, cPropertyActiveDocument, objDoc)
for o in m do
     {
 if(oleCopy(o))
        {
 
 oleGet(cobjWord, "Selection", objSel)
 clear(oleAutoArgs);

oleMethod(objSel, "Paste")

 string error1=oleGet(objDoc,cPropertyTables, objTables)
if(null error1)
print error1"\n"
 oleGet(objTables, cPropertyCount, count);
print count "\n"
 clear oleAutoArgs
 put(oleAutoArgs,1)
 string error=oleMethod(objTables,cMethodItem, oleAutoArgs, objTable);
 if(!null error)
print error"\n"

}}

 

 

 

Re: Exporting OleObject into word document
woodpryan - Tue Jun 02 11:19:01 EDT 2015

I don't know for sure that this will exactly work because I don't have any test cases in my DOORS client, and I have neither the time nor the inclination to create any. But, this is the code I've been trying to tell you how to write. I hope it works for you.

 #include <utils/ole.inc>
 
string cMethodExpand = "Expand";
string cMethodCopy   = "Copy";

OleAutoObj objWord   = null;
OleAutoObj objDocs   = null;
OleAutoObj objDoc    = null;
OleAutoObj objSel    = null;
    
OleAutoArgs objArgBlock = create();

void cleanUp()
{
    closeIfNonNull(objWord);
    closeIfNonNull(objDocs);
    closeIfNonNull(objDoc);
    closeIfNonNull(objSel);
    
    delete(objArgBlock);
}

void logError(string str)
{
    print("Error: " str "\n");
}

void logMessage(string str)
{
    print(str "\n");
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        return;
    }
    
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

/*  Function:   copySelection()
    Purpose:    Calls the MS Word copy function on the current selection.
*/
void copySelection()
{
    oleMethod(objSel, cMethodCopy);
}

/*  Funciton:   paste()
    Purpose:    calls the MS Word paste function on the current selection.
                Anything in the clipboard at the time is pasted into the document
*/
void paste()
{ 
    oleMethod(objSel, cMethodPaste);
}

/*  Function:   convertSingleCellTablesToObjText()
    Purpose:    This function expects that Objects in the currently open for edit Module
                are all ole tables with a single cell. It loops through all the Objects,
                copies the table, pastes the table to MS Word, gets the text from the table,
                then sets the Object's "Object Text" to be what the text in the cell is.
                Deletes each Table from MS Word after the change.
                Creates an instance of MS Word.
*/
void convertSingleCellTablesToObjText()
{
    Object obj     = null;
    Module mod     = current();//must be open for edit
    string errMess = null;
    string sText   = null;
    
    
    OleAutoObj objTable  = null;
    OleAutoObj objCell   = null;
    OleAutoObj objRange  = null;
    
    objWord = oleCreateAutoObject("Word.Application");
 
    if(null == objWord)
    {
        warningBox("Unable to start Microsoft Word");
        halt();
    }
 
    makeVisible(objWord);
    oleGet(objWord, cPropertyDocuments, objDocs);

    for obj in mod do
    {
        if(oleCopy(obj))
            logMessage("Succesfully copied");
        else
        {
            logError("Could not copy ole from the Object.");
            continue;
        }
 
        //paste the table
        paste();
        
        //get the table from word
        objTable = getWordTable(1);
        //get the range of the single-cell table
        objRange = getRange(objTable, 1, 1, 1, 1);
        //get the text from the cell
        sText = getRangeText(objRange);
        
        obj."Object Text" = sText;
        //select all of the document
        expandSelection(wdStory);
        //delete the tables in the selection
        deleteTablesInSelection();
    }
    
    //release memory
    closeIfNonNull(objTable);
    closeIfNonNull(objCell);
    closeIfNonNull(objRange);
       
    cleanUp();
}

 

Re: Exporting OleObject into word document
dhars_priya - Wed Jun 03 04:51:10 EDT 2015

woodpryan - Tue Jun 02 11:19:01 EDT 2015

I don't know for sure that this will exactly work because I don't have any test cases in my DOORS client, and I have neither the time nor the inclination to create any. But, this is the code I've been trying to tell you how to write. I hope it works for you.

 #include <utils/ole.inc>
 
string cMethodExpand = "Expand";
string cMethodCopy   = "Copy";

OleAutoObj objWord   = null;
OleAutoObj objDocs   = null;
OleAutoObj objDoc    = null;
OleAutoObj objSel    = null;
    
OleAutoArgs objArgBlock = create();

void cleanUp()
{
    closeIfNonNull(objWord);
    closeIfNonNull(objDocs);
    closeIfNonNull(objDoc);
    closeIfNonNull(objSel);
    
    delete(objArgBlock);
}

void logError(string str)
{
    print("Error: " str "\n");
}

void logMessage(string str)
{
    print(str "\n");
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        return;
    }
    
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

/*  Function:   copySelection()
    Purpose:    Calls the MS Word copy function on the current selection.
*/
void copySelection()
{
    oleMethod(objSel, cMethodCopy);
}

/*  Funciton:   paste()
    Purpose:    calls the MS Word paste function on the current selection.
                Anything in the clipboard at the time is pasted into the document
*/
void paste()
{ 
    oleMethod(objSel, cMethodPaste);
}

/*  Function:   convertSingleCellTablesToObjText()
    Purpose:    This function expects that Objects in the currently open for edit Module
                are all ole tables with a single cell. It loops through all the Objects,
                copies the table, pastes the table to MS Word, gets the text from the table,
                then sets the Object's "Object Text" to be what the text in the cell is.
                Deletes each Table from MS Word after the change.
                Creates an instance of MS Word.
*/
void convertSingleCellTablesToObjText()
{
    Object obj     = null;
    Module mod     = current();//must be open for edit
    string errMess = null;
    string sText   = null;
    
    
    OleAutoObj objTable  = null;
    OleAutoObj objCell   = null;
    OleAutoObj objRange  = null;
    
    objWord = oleCreateAutoObject("Word.Application");
 
    if(null == objWord)
    {
        warningBox("Unable to start Microsoft Word");
        halt();
    }
 
    makeVisible(objWord);
    oleGet(objWord, cPropertyDocuments, objDocs);

    for obj in mod do
    {
        if(oleCopy(obj))
            logMessage("Succesfully copied");
        else
        {
            logError("Could not copy ole from the Object.");
            continue;
        }
 
        //paste the table
        paste();
        
        //get the table from word
        objTable = getWordTable(1);
        //get the range of the single-cell table
        objRange = getRange(objTable, 1, 1, 1, 1);
        //get the text from the cell
        sText = getRangeText(objRange);
        
        obj."Object Text" = sText;
        //select all of the document
        expandSelection(wdStory);
        //delete the tables in the selection
        deleteTablesInSelection();
    }
    
    //release memory
    closeIfNonNull(objTable);
    closeIfNonNull(objCell);
    closeIfNonNull(objRange);
       
    cleanUp();
}

 

Hi Woodpryan.

Thank you for your support.

Now also i am getting  null objTable why i dont no?

Could you check why i am getting null objTable and i used the same code which you have sent.

I am not able to proceed further from that step for the past one week i am getting same error .

Re: Exporting OleObject into word document
dhars_priya - Wed Jun 03 06:55:08 EDT 2015

woodpryan - Tue Jun 02 11:19:01 EDT 2015

I don't know for sure that this will exactly work because I don't have any test cases in my DOORS client, and I have neither the time nor the inclination to create any. But, this is the code I've been trying to tell you how to write. I hope it works for you.

 #include <utils/ole.inc>
 
string cMethodExpand = "Expand";
string cMethodCopy   = "Copy";

OleAutoObj objWord   = null;
OleAutoObj objDocs   = null;
OleAutoObj objDoc    = null;
OleAutoObj objSel    = null;
    
OleAutoArgs objArgBlock = create();

void cleanUp()
{
    closeIfNonNull(objWord);
    closeIfNonNull(objDocs);
    closeIfNonNull(objDoc);
    closeIfNonNull(objSel);
    
    delete(objArgBlock);
}

void logError(string str)
{
    print("Error: " str "\n");
}

void logMessage(string str)
{
    print(str "\n");
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        return;
    }
    
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

/*  Function:   copySelection()
    Purpose:    Calls the MS Word copy function on the current selection.
*/
void copySelection()
{
    oleMethod(objSel, cMethodCopy);
}

/*  Funciton:   paste()
    Purpose:    calls the MS Word paste function on the current selection.
                Anything in the clipboard at the time is pasted into the document
*/
void paste()
{ 
    oleMethod(objSel, cMethodPaste);
}

/*  Function:   convertSingleCellTablesToObjText()
    Purpose:    This function expects that Objects in the currently open for edit Module
                are all ole tables with a single cell. It loops through all the Objects,
                copies the table, pastes the table to MS Word, gets the text from the table,
                then sets the Object's "Object Text" to be what the text in the cell is.
                Deletes each Table from MS Word after the change.
                Creates an instance of MS Word.
*/
void convertSingleCellTablesToObjText()
{
    Object obj     = null;
    Module mod     = current();//must be open for edit
    string errMess = null;
    string sText   = null;
    
    
    OleAutoObj objTable  = null;
    OleAutoObj objCell   = null;
    OleAutoObj objRange  = null;
    
    objWord = oleCreateAutoObject("Word.Application");
 
    if(null == objWord)
    {
        warningBox("Unable to start Microsoft Word");
        halt();
    }
 
    makeVisible(objWord);
    oleGet(objWord, cPropertyDocuments, objDocs);

    for obj in mod do
    {
        if(oleCopy(obj))
            logMessage("Succesfully copied");
        else
        {
            logError("Could not copy ole from the Object.");
            continue;
        }
 
        //paste the table
        paste();
        
        //get the table from word
        objTable = getWordTable(1);
        //get the range of the single-cell table
        objRange = getRange(objTable, 1, 1, 1, 1);
        //get the text from the cell
        sText = getRangeText(objRange);
        
        obj."Object Text" = sText;
        //select all of the document
        expandSelection(wdStory);
        //delete the tables in the selection
        deleteTablesInSelection();
    }
    
    //release memory
    closeIfNonNull(objTable);
    closeIfNonNull(objCell);
    closeIfNonNull(objRange);
       
    cleanUp();
}

 

For u it is working right why it is not coming for me.What i a made wrong? objTables is not null but objTable is null

And i tried another way i created a vba script to get the single celled table content and i pasted that values in text file .

i called that macro from dxl script but the contents are not pasted in the text file.

Re: Exporting OleObject into word document
dhars_priya - Wed Jun 03 07:27:39 EDT 2015

woodpryan - Tue Jun 02 11:19:01 EDT 2015

I don't know for sure that this will exactly work because I don't have any test cases in my DOORS client, and I have neither the time nor the inclination to create any. But, this is the code I've been trying to tell you how to write. I hope it works for you.

 #include <utils/ole.inc>
 
string cMethodExpand = "Expand";
string cMethodCopy   = "Copy";

OleAutoObj objWord   = null;
OleAutoObj objDocs   = null;
OleAutoObj objDoc    = null;
OleAutoObj objSel    = null;
    
OleAutoArgs objArgBlock = create();

void cleanUp()
{
    closeIfNonNull(objWord);
    closeIfNonNull(objDocs);
    closeIfNonNull(objDoc);
    closeIfNonNull(objSel);
    
    delete(objArgBlock);
}

void logError(string str)
{
    print("Error: " str "\n");
}

void logMessage(string str)
{
    print(str "\n");
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        return;
    }
    
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

/*  Function:   copySelection()
    Purpose:    Calls the MS Word copy function on the current selection.
*/
void copySelection()
{
    oleMethod(objSel, cMethodCopy);
}

/*  Funciton:   paste()
    Purpose:    calls the MS Word paste function on the current selection.
                Anything in the clipboard at the time is pasted into the document
*/
void paste()
{ 
    oleMethod(objSel, cMethodPaste);
}

/*  Function:   convertSingleCellTablesToObjText()
    Purpose:    This function expects that Objects in the currently open for edit Module
                are all ole tables with a single cell. It loops through all the Objects,
                copies the table, pastes the table to MS Word, gets the text from the table,
                then sets the Object's "Object Text" to be what the text in the cell is.
                Deletes each Table from MS Word after the change.
                Creates an instance of MS Word.
*/
void convertSingleCellTablesToObjText()
{
    Object obj     = null;
    Module mod     = current();//must be open for edit
    string errMess = null;
    string sText   = null;
    
    
    OleAutoObj objTable  = null;
    OleAutoObj objCell   = null;
    OleAutoObj objRange  = null;
    
    objWord = oleCreateAutoObject("Word.Application");
 
    if(null == objWord)
    {
        warningBox("Unable to start Microsoft Word");
        halt();
    }
 
    makeVisible(objWord);
    oleGet(objWord, cPropertyDocuments, objDocs);

    for obj in mod do
    {
        if(oleCopy(obj))
            logMessage("Succesfully copied");
        else
        {
            logError("Could not copy ole from the Object.");
            continue;
        }
 
        //paste the table
        paste();
        
        //get the table from word
        objTable = getWordTable(1);
        //get the range of the single-cell table
        objRange = getRange(objTable, 1, 1, 1, 1);
        //get the text from the cell
        sText = getRangeText(objRange);
        
        obj."Object Text" = sText;
        //select all of the document
        expandSelection(wdStory);
        //delete the tables in the selection
        deleteTablesInSelection();
    }
    
    //release memory
    closeIfNonNull(objTable);
    closeIfNonNull(objCell);
    closeIfNonNull(objRange);
       
    cleanUp();
}

 

How to iterate only the current view in the module?

Re: Exporting OleObject into word document
woodpryan - Wed Jun 03 11:28:05 EDT 2015

First, let me say this is the last time I will reply to this thread. It has gone long enough for me. Maybe someone else will pick this up, but I can't do your job for you.

I never said what I gave you would work. In fact, I said the opposite. I clearly stated, "I don't know for sure that this will exactly work because I don't have any test cases in my DOORS client, and I have neither the time nor the inclination to create any."

I did take some time to run some dummy tests with some tables I happened to create in Word yesterday, and found the problem. I used the wrong variable from which to retrieve the tables. Should have been objDoc instead of objSel. But, also, I forgot to call the createNewDocument function.

As far as iterating through the Objects in the current View go, the for Object in Module loop goes through only the Objects in the current view.

Here is the revised code, and this should at least work to paste the tables into Word and then get the text of the cell.

 

#include <utils/ole.inc>
 
string cMethodExpand   = "Expand";
string cMethodCopy     = "Copy";

OleAutoObj objWord   = null;
OleAutoObj objDocs   = null;
OleAutoObj objDoc    = null;
OleAutoObj objSel    = null;
    
OleAutoArgs objArgBlock = create();

void cleanUp()
{
    closeIfNonNull(objWord);
    closeIfNonNull(objDocs);
    closeIfNonNull(objDoc);
    closeIfNonNull(objSel);
    
    delete(objArgBlock);
}

void logError(string str)
{
    print("Error: " str "\n");
}

void logMessage(string str)
{
    print(str "\n");
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        return;
    }
    
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    int        count     = 0;
    
    oleGet(objDoc, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

/*  Function:   copySelection()
    Purpose:    Calls the MS Word copy function on the current selection.
*/
void copySelection()
{
    oleMethod(objSel, cMethodCopy);
}

/*  Funciton:   paste()
    Purpose:    calls the MS Word paste function on the current selection.
                Anything in the clipboard at the time is pasted into the document
*/
void paste()
{ 
    oleMethod(objSel, cMethodPaste);
}

/*  Function:   convertSingleCellTablesToObjText()
    Purpose:    This function expects that Objects in the currently open for edit Module
                are all ole tables with a single cell. It loops through all the Objects,
                copies the table, pastes the table to MS Word, gets the text from the table,
                then sets the Object's "Object Text" to be what the text in the cell is.
                Deletes each Table from MS Word after the change.
                Creates an instance of MS Word.
*/
void convertSingleCellTablesToObjText()
{
    Object obj     = null;
    Module mod     = current();//must be open for edit
    string errMess = null;
    string sText   = null;
    
    
    OleAutoObj objTable  = null;
    OleAutoObj objCell   = null;
    OleAutoObj objRange  = null;
    
    objWord = oleCreateAutoObject("Word.Application");
 
    if(null == objWord)
    {
        warningBox("Unable to start Microsoft Word");
        halt();
    }
 
    makeVisible(objWord);
    oleGet(objWord, cPropertyDocuments, objDocs);
    
    createNewDocument();
    
    for obj in mod do
    {
        if(oleCopy(obj))
            logMessage("Succesfully copied");
        else
        {
            logError("Could not copy ole from the Object.");
            continue;
        }
 
        //paste the table
        paste();
        
        //get the table from word
        objTable = getWordTable(1);
        //get the range of the single-cell table
        objRange = getRange(objTable, 2, 1, 2, 1);
        //get the text from the cell
        sText = getRangeText(objRange);
        print(sText "\n");
        
        obj."Object Text" = sText;
        //select all of the document
        expandSelection(wdStory);
        //delete the tables in the selection
        deleteTablesInSelection();
    }
    
    //release memory
    closeIfNonNull(objTable);
    closeIfNonNull(objCell);
    closeIfNonNull(objRange);
       
    cleanUp();
}

convertSingleCellTablesToObjText();

 

Re: Exporting OleObject into word document
dhars_priya - Thu Jun 04 08:28:26 EDT 2015

woodpryan - Wed Jun 03 11:28:05 EDT 2015

First, let me say this is the last time I will reply to this thread. It has gone long enough for me. Maybe someone else will pick this up, but I can't do your job for you.

I never said what I gave you would work. In fact, I said the opposite. I clearly stated, "I don't know for sure that this will exactly work because I don't have any test cases in my DOORS client, and I have neither the time nor the inclination to create any."

I did take some time to run some dummy tests with some tables I happened to create in Word yesterday, and found the problem. I used the wrong variable from which to retrieve the tables. Should have been objDoc instead of objSel. But, also, I forgot to call the createNewDocument function.

As far as iterating through the Objects in the current View go, the for Object in Module loop goes through only the Objects in the current view.

Here is the revised code, and this should at least work to paste the tables into Word and then get the text of the cell.

 

#include <utils/ole.inc>
 
string cMethodExpand   = "Expand";
string cMethodCopy     = "Copy";

OleAutoObj objWord   = null;
OleAutoObj objDocs   = null;
OleAutoObj objDoc    = null;
OleAutoObj objSel    = null;
    
OleAutoArgs objArgBlock = create();

void cleanUp()
{
    closeIfNonNull(objWord);
    closeIfNonNull(objDocs);
    closeIfNonNull(objDoc);
    closeIfNonNull(objSel);
    
    delete(objArgBlock);
}

void logError(string str)
{
    print("Error: " str "\n");
}

void logMessage(string str)
{
    print(str "\n");
}

/*  Function:   createNewDocument()
    Purpose:    creates a new MS Word document, sets it active,
                and gets a new handle on its selection Object
*/
void createNewDocument()
{
    //create the new document
    oleMethod(objDocs, cMethodAdd);
    //set the objDoc global to be the new, active document
    oleGet(objWord, cPropertyActiveDocument, objDoc);
    //get a new handle on the selection Object
    oleGet(objWord, cPropertySelection, objSel);
}

/* Function:   getRange(OleAutoObj, int, int, int, int)
   Purpose:    creates a range out of the table from the beginning of the start row/column
               to the end of the end row/column.
   Parameters: The table, the start row, the start column,
               the end row, the end column
   Return:     the range if one was succesfully created. null otherwise
*/
OleAutoObj getRange(OleAutoObj theTable, int sRow, int sCol, int eRow, eCol)
{
    /*The VBA code we need to replicate in DXL
    myTable = ActiveDocument.Tables(1)
    myRange = ActiveDocument.Range(myTable.Cell(1, 1).Range.Start, myTable.Cell(1, 2).Range.End)
    myRange.Cells.Merge
    */
    
    OleAutoObj objRange   = null;
    OleAutoObj objCell    = null;
    OleAutoObj tmpRange   = null;
    int        startRange = 0;
    int        endRange   = 0;
    
    clear(objArgBlock);
    put(objArgBlock, sRow);
    put(objArgBlock, sCol);
    oleMethod(theTable, cMethodCell, objArgBlock, objCell);
    if(null != objCell)
    {
        oleGet(objCell, cPropertyRange, tmpRange);
        if(null!= tmpRange)
        {
            oleGet(tmpRange, cPropertyStart, startRange);
            if(startRange >= 0)
            {
                clear(objArgBlock);
                put(objArgBlock, eRow);
                put(objArgBlock, eCol);
                oleMethod(theTable, cMethodCell, objArgBlock, objCell);
                if(null != objCell)
                {
                    oleGet(objCell, cPropertyRange, tmpRange);
                    if(null != tmpRange)
                    {
                        oleGet(tmpRange, cPropertyEnd, endRange);
                        if(0 != endRange)
                        {
                            clear(objArgBlock);
                            put(objArgBlock, startRange);
                            put(objArgBlock, endRange);
                            oleMethod(objDoc, cMethodRange, objArgBlock, objRange);
                        }
                        else
                            logError("getRange found 0 for endRange.");
                    }
                    else
                        logError("unable to get a tmpRange from the end cell.");
                }
                else
                    logError("objCell is null in getRange.");
            }
            else
                logError("getRange found invalid startRange.");
        }
        else
            logError("Unable to get a tmpRange from the start cell.");
    }
    if(null == objRange)
        logError("Unable to get the specified range!");
        
    closeIfNonNull(tmpRange);
    closeIfNonNull(objCell);
    
    return objRange;
}

/*  Function:   getRangeText(OleAutoObj)
    Purpose:    gets the text out of the specified Range as a string.
    Parameters: the range
    Return:     the text in the range as a string
*/
string getRangeText(OleAutoObj objRange)
{
    string rText = null;
    
    if(null != objRange)
    {
        oleGet(objRange, cPropertyText, rText);
    }
    else
        logError("The Range Object passed to getRangeText was null.\n");
    
    return rText;
}

/*  Function:   expandSelection(int)
    Purpose:    takes the selection Object (the cursor) in the current
                MS Word document and expands it based on the given unit
                type. May select the entire document, a paragraph or character
    Parameters: the unit type, may be any of the WdUnitType constant integers
                defined in ThaadOleDotInc
*/
void expandSelection(int wdUnit)
{
    if(null == objSel)
    {
        logError("There is no selection to use for expandSelection.");
        return;
    }
    
    clear(objArgBlock);
    put(objArgBlock, wdUnit);
    oleMethod(objSel, cMethodExpand, objArgBlock);
}

/*  Function:   deleteTablesInSelection()
    Purpose:    deletes any tables in the current selection
                in the document.
*/
void deleteTablesInSelection()
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        count     = 0;
    int        i         = 0;
    
    oleGet(objSel, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for deleteTablesInSelection.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        for(i = 1; i <= count; i++)
        {
            clear(objArgBlock);
            put(objArgBlock, count);
            oleMethod(objTables, cMethodItem, objArgBlock, objTable);
            if(null == objTable)
            {
                logError("Could not retrieve the table " count " from the Tables collection for deleteTablesInSelection.");
                closeIfNonNull(objTables);
                return;
            }
            oleMethod(objTable, cMethodDelete);
        }
    }
    
    closeIfNonNull(objTables);
    closeIfNonNull(objTable);
}

/*  Function:   getWordTable(int)
    Purpose:    retrieves a table from the document having the given index
    Return:     the table
*/
OleAutoObj getWordTable(int index)
{
    OleAutoObj objTables = null;
    OleAutoObj objTable  = null;
    int        i         = 0;
    int        count     = 0;
    
    oleGet(objDoc, cPropertyTables, objTables);
    if(null == objTables)
    {
        logError("Could not retrieve the collection of Tables from the selection for getWordTable.");
        return;
    }
    
    oleGet(objTables, cPropertyCount, count);
    if(count > 0)
    {
        clear(objArgBlock);
        put(objArgBlock, index);
        oleMethod(objTables, cMethodItem, objArgBlock, objTable);
        if(null == objTable)
        {
            logError("Could not retrieve the table " index " from the Tables collection for getWordTable.");
            closeIfNonNull(objTables);
            return;
        }
    }
    
    closeIfNonNull(objTables);
    
    return objTable;
}

/*  Function:   copySelection()
    Purpose:    Calls the MS Word copy function on the current selection.
*/
void copySelection()
{
    oleMethod(objSel, cMethodCopy);
}

/*  Funciton:   paste()
    Purpose:    calls the MS Word paste function on the current selection.
                Anything in the clipboard at the time is pasted into the document
*/
void paste()
{ 
    oleMethod(objSel, cMethodPaste);
}

/*  Function:   convertSingleCellTablesToObjText()
    Purpose:    This function expects that Objects in the currently open for edit Module
                are all ole tables with a single cell. It loops through all the Objects,
                copies the table, pastes the table to MS Word, gets the text from the table,
                then sets the Object's "Object Text" to be what the text in the cell is.
                Deletes each Table from MS Word after the change.
                Creates an instance of MS Word.
*/
void convertSingleCellTablesToObjText()
{
    Object obj     = null;
    Module mod     = current();//must be open for edit
    string errMess = null;
    string sText   = null;
    
    
    OleAutoObj objTable  = null;
    OleAutoObj objCell   = null;
    OleAutoObj objRange  = null;
    
    objWord = oleCreateAutoObject("Word.Application");
 
    if(null == objWord)
    {
        warningBox("Unable to start Microsoft Word");
        halt();
    }
 
    makeVisible(objWord);
    oleGet(objWord, cPropertyDocuments, objDocs);
    
    createNewDocument();
    
    for obj in mod do
    {
        if(oleCopy(obj))
            logMessage("Succesfully copied");
        else
        {
            logError("Could not copy ole from the Object.");
            continue;
        }
 
        //paste the table
        paste();
        
        //get the table from word
        objTable = getWordTable(1);
        //get the range of the single-cell table
        objRange = getRange(objTable, 2, 1, 2, 1);
        //get the text from the cell
        sText = getRangeText(objRange);
        print(sText "\n");
        
        obj."Object Text" = sText;
        //select all of the document
        expandSelection(wdStory);
        //delete the tables in the selection
        deleteTablesInSelection();
    }
    
    //release memory
    closeIfNonNull(objTable);
    closeIfNonNull(objCell);
    closeIfNonNull(objRange);
       
    cleanUp();
}

convertSingleCellTablesToObjText();

 

Thank you.. Sorry ..

Thanks a lot